import('@esbuild-kit/cjs-loader')

import { execSync } from 'node:child_process'

const scopes = [
  'docs',
  'play',
  'project',
  'core',
  'style',
  'ci',
  'dev',
  'deploy',
  'other',
  'typography',
  'color',
  'border',
  'var',
  'ssr'
]

const gitStatus = execSync('git status --porcelain || true')
  .toString()
  .trim()
  .split('\n')

const scopeComplete = gitStatus
  .find(r => ~r.indexOf('M  packages'))
  ?.replace(/\//g, '%%')
  ?.match(/packages%%((\w|-)*)/)?.[1]

const subjectComplete = gitStatus
  .find(r => ~r.indexOf('M  packages/components'))
  ?.replace(/\//g, '%%')
  ?.match(/packages%%components%%((\w|-)*)/)?.[1]

export default {
  rules: {
    /**
     * type[scope]: [function] description
     *      ^^^^^
     */
    'scope-enum': [2, 'always', scopes],
    /**
     * type[scope]: [function] description
     *
     * ^^^^^^^^^^^^^^ empty line.
     * - Something here
     */
    'body-leading-blank': [1, 'always'],
    /**
     * type[scope]: [function] description
     *
     * - something here
     *
     * ^^^^^^^^^^^^^^
     */
    'footer-leading-blank': [1, 'always'],
    /**
     * type[scope]: [function] description [No more than 72 characters]
     *      ^^^^^
     */
    'header-max-length': [2, 'always', 72],
    'scope-case': [2, 'always', 'lower-case'],
    'subject-case': [
      1,
      'never',
      ['sentence-case', 'start-case', 'pascal-case', 'upper-case']
    ],
    'subject-empty': [2, 'never'],
    'subject-full-stop': [2, 'never', '.'],
    'type-case': [2, 'always', 'lower-case'],
    'type-empty': [2, 'never'],
    /**
     * type[scope]: [function] description
     * ^^^^
     */
    'type-enum': [
      2,
      'always',
      /**
       * commit -m 允许的标签
       * feat: 新功能
       * build: 影响构建组件的更改，如构建工具、依赖项、项目版本等;
       * chore: 比如构建流程、辅助工具等的更改。
       * docs: 文档相关的提交
       * fix: 修补错误的更改
       * release: 发布项目时的提交
       * style: 代码格式的更改(空格、格式、缺少分号等)
       * refactor：重构代码
       * ci: 持续集成/持续部署相关任务
       * test: 更新测试用例之类
       */
      [
        'feat',
        'build',
        'chore',
        'docs',
        'fix',
        'release',
        'style',
        'refactor',
        'test',
        'ci'
      ]
    ]
  },
  prompt: {
    defaultScope: scopeComplete,
    customScopesAlign: scopeComplete ? 'bottom' : 'top',
    defaultSubject: subjectComplete && `[${subjectComplete}] `,
    allowCustomIssuePrefixs: false,
    allowEmptyIssuePrefixs: false
  }
}
